package google.usingblobstore;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A servlet that returns a one time use url to upload a blob to.
 * 
 * @author eschoeffler@google.com (Eric Schoeffler)
 */
@SuppressWarnings("serial")
public class GetUploadUrlServlet extends HttpServlet {
  
  private BlobstoreService blobstoreService;
  
  public GetUploadUrlServlet() {
    blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
  }
  
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    String uploadUrl = blobstoreService.createUploadUrl("/postUpload");
    PrintWriter pw = new PrintWriter(resp.getOutputStream());
    pw.println(uploadUrl);
    pw.close();
  }
}